﻿using FH.Application.BannerApp.Dtos;
using FH.Application.NewsApp;
using FH.Application.NewsApp.Dtos;
using FH.Application.PorductApp.Dtos;
using FH.Application.UserApp;
using FH.Core.Entities;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;

namespace WebSite.Controllers
{
    public class AdminController : FonourControllerBase
    {
        private readonly INewsAppService _service;
        private readonly IProductAppService _productService;
        private readonly ISettingService _settingService;
        public AdminController(INewsAppService service
            , IProductAppService productService
            , ISettingService settingService)
        {
            _service = service;
            _productService = productService;
            _settingService = settingService;
        }

        public IActionResult Index()
        {
            return View();
        }
        #region 新闻管理
        public IActionResult News()
        {
            return View();
        }
        public IActionResult GetNewsPage( int startPage, int pageSize)
        {
            int rowCount = 0;
            var result = _service.GetNewsPage(startPage, pageSize, out rowCount);
          
            return Json(new
            {
                rowCount = rowCount,
                pageCount = Math.Ceiling(Convert.ToDecimal(rowCount) / pageSize),
                rows = result,
            });
        }

        public IActionResult EditNews(NewsDto dto)
        {
            try
            {
                if (dto.Id == Guid.Empty)
                {
                    dto.Id = Guid.NewGuid();
                }
                var user = _service.InsertOrUpdate(dto);
                return Json(new { Result = "Success" });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "Faild", Message = ex.Message });

            }
        }

        public IActionResult DeleteNewss(string ids)
        {
            try
            {
                string[] idArray = ids.Split(',');
                List<Guid> delIds = new List<Guid>();
                foreach (string id in idArray)
                {
                    delIds.Add(Guid.Parse(id));
                }
                _service.DeleteBatch(delIds);
                return Json(new
                {
                    Result = "Success"
                });
            }
            catch (Exception ex)
            {
                return Json(new
                {
                    Result = "Faild",
                    Message = ex.Message
                });
            }
        }
        public IActionResult DeleteNews(Guid id)
        {
            try
            {
                _service.Delete(id);
                return Json(new
                {
                    Result = "Success"
                });
            }
            catch (Exception ex)
            {
                return Json(new
                {
                    Result = "Faild",
                    Message = ex.Message
                });
            }
        }
        public IActionResult GetNews(Guid id)
        {
            var dto = _service.Get(id);
            return Json(dto);
        }
        #endregion
        #region 分类管理
        public IActionResult Categorys()
        {
            return View();
        }
        public IActionResult GetCategoryPage(int startPage, int pageSize)
        {
            int rowCount = 0;
            var result = _productService.GetCategoryDtos(startPage, pageSize, out rowCount);
            return Json(new
            {
                rowCount = rowCount,
                pageCount = Math.Ceiling(Convert.ToDecimal(rowCount) / pageSize),
                rows = result,
            });
        }

        public IActionResult EditCategory(CategoryDto dto)
        {
            try
            {
                if (dto.Id == Guid.Empty)
                {
                    dto.Id = Guid.NewGuid();
                }
                var user = _productService.InsertOrUpdateCategory(dto);
                return Json(new { Result = "Success" });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "Faild", Message = ex.Message });

            }
        }

        public IActionResult DeleteCategorys(string ids)
        {
            try
            {
                string[] idArray = ids.Split(',');
                List<Guid> delIds = new List<Guid>();
                foreach (string id in idArray)
                {
                    delIds.Add(Guid.Parse(id));
                }
                _productService.DeleteBatchCategory(delIds);
                return Json(new
                {
                    Result = "Success"
                });
            }
            catch (Exception ex)
            {
                return Json(new
                {
                    Result = "Faild",
                    Message = ex.Message
                });
            }
        }
        public IActionResult DeleteCategory(Guid id)
        {
            try
            {
                _productService.DeleteCategory(id);
                return Json(new
                {
                    Result = "Success"
                });
            }
            catch (Exception ex)
            {
                return Json(new
                {
                    Result = "Faild",
                    Message = ex.Message
                });
            }
        }
        public IActionResult GetCategory(Guid id)
        {
            var dto = _productService.GetCategory(id);
            return Json(dto);
        }
        #endregion
        #region 产品管理
        public IActionResult Products()
        {
            return View();
        }
        public IActionResult GetProductPage(Guid? categoryId, int startPage, int pageSize)
        {
            int rowCount = 0;
            var result = _productService.GetProducts(categoryId,startPage, pageSize, out rowCount);
            if (result != null && result.Any())
            {
                Guid[] ids = result.Where(r => r.CategoryId.HasValue).Select(r => r.CategoryId.Value).ToArray();
                IEnumerable<CategoryDto> categorys = null;
                if (ids != null && ids.Any())
                {
                    categorys = _productService.GetCategory(ids);
                }
                if (categorys != null && categorys.Any())
                {
                    foreach(ProductDto item in result)
                    {
                        if (item.CategoryId.HasValue)
                        {
                            item.CategoryName = categorys.FirstOrDefault(r => r.Id == item.CategoryId)?.Name;
                        }
                        
                    }
                }
            }
            int total = 0;
            var roles = _productService.GetCategoryDtos(1, 100, out total);
            return Json(new
            {
                rowCount = rowCount,
                pageCount = Math.Ceiling(Convert.ToDecimal(rowCount) / pageSize),
                rows = result,
                roles = roles
            });
        }
        public IActionResult EditProduct(ProductDto dto)
        {
            try
            {
                if (dto.Id == Guid.Empty)
                {
                    dto.Id = Guid.NewGuid();
                }
                var user = _productService.InsertOrUpdate(dto);
                return Json(new { Result = "Success" });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "Faild", Message = ex.Message });

            }
        }

        public IActionResult DeleteProducts(string ids)
        {
            try
            {
                string[] idArray = ids.Split(',');
                List<Guid> delIds = new List<Guid>();
                foreach (string id in idArray)
                {
                    delIds.Add(Guid.Parse(id));
                }
                _productService.DeleteBatch(delIds);
                return Json(new
                {
                    Result = "Success"
                });
            }
            catch (Exception ex)
            {
                return Json(new
                {
                    Result = "Faild",
                    Message = ex.Message
                });
            }
        }
        public IActionResult DeleteProduct(Guid id)
        {
            try
            {
                _productService.Delete(id);
                return Json(new
                {
                    Result = "Success"
                });
            }
            catch (Exception ex)
            {
                return Json(new
                {
                    Result = "Faild",
                    Message = ex.Message
                });
            }
        }
        public IActionResult GetProduct(Guid id)
        {
            var dto = _productService.Get(id);
            return Json(dto);
        }
        #endregion
        #region Banner管理
        public IActionResult Banners()
        {
            return View();
        }
        public IActionResult GetBannerPage(int startPage, int pageSize)
        {
            int rowCount = 0;
            var result = _settingService.GetBannerPage(startPage, pageSize, out rowCount);
            return Json(new
            {
                rowCount = rowCount,
                pageCount = Math.Ceiling(Convert.ToDecimal(rowCount) / pageSize),
                rows = result,
            });
        }

        public IActionResult EditBanner(BannerDto dto)
        {
            try
            {
                if (dto.Id == Guid.Empty)
                {
                    dto.Id = Guid.NewGuid();
                }
                var user = _settingService.InsertOrUpdate(dto);
                return Json(new { Result = "Success" });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "Faild", Message = ex.Message });

            }
        }

        public IActionResult DeleteBanners(string ids)
        {
            try
            {
                string[] idArray = ids.Split(',');
                List<Guid> delIds = new List<Guid>();
                foreach (string id in idArray)
                {
                    delIds.Add(Guid.Parse(id));
                }
                _settingService.DeleteBatch(delIds);
                return Json(new
                {
                    Result = "Success"
                });
            }
            catch (Exception ex)
            {
                return Json(new
                {
                    Result = "Faild",
                    Message = ex.Message
                });
            }
        }
        public IActionResult DeleteBanner(Guid id)
        {
            try
            {
                _settingService.Delete(id);
                return Json(new
                {
                    Result = "Success"
                });
            }
            catch (Exception ex)
            {
                return Json(new
                {
                    Result = "Faild",
                    Message = ex.Message
                });
            }
        }
        public IActionResult GetBanner(Guid id)
        {
            var dto = _settingService.Get(id);
            return Json(dto);
        }
        #endregion
        #region 客户管理
        public IActionResult Customers()
        {
            return View();
        }
        public IActionResult GetCustomerPage(int startPage, int pageSize)
        {
            int rowCount = 0;
            var result = _settingService.GetCustomerPage(startPage, pageSize, out rowCount);
            return Json(new
            {
                rowCount = rowCount,
                pageCount = Math.Ceiling(Convert.ToDecimal(rowCount) / pageSize),
                rows = result,
            });
        }

        public IActionResult EditCustomer(CustomerDto dto)
        {
            try
            {
                if (dto.Id == Guid.Empty)
                {
                    dto.Id = Guid.NewGuid();
                }
                var user = _settingService.InsertOrUpdateCustomer(dto);
                return Json(new { Result = "Success" });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "Faild", Message = ex.Message });

            }
        }

        public IActionResult DeleteCustomers(string ids)
        {
            try
            {
                string[] idArray = ids.Split(',');
                List<Guid> delIds = new List<Guid>();
                foreach (string id in idArray)
                {
                    delIds.Add(Guid.Parse(id));
                }
                _settingService.DeleteBatchCustomer(delIds);
                return Json(new
                {
                    Result = "Success"
                });
            }
            catch (Exception ex)
            {
                return Json(new
                {
                    Result = "Faild",
                    Message = ex.Message
                });
            }
        }
        public IActionResult DeleteCustomer(Guid id)
        {
            try
            {
                _settingService.DeleteCustomer(id);
                return Json(new
                {
                    Result = "Success"
                });
            }
            catch (Exception ex)
            {
                return Json(new
                {
                    Result = "Faild",
                    Message = ex.Message
                });
            }
        }
        public IActionResult GetCustomer(Guid id)
        {
            var dto = _settingService.GetCustomer(id);
            return Json(dto);
        }
        #endregion
    }
}